package com.chenfan.process.util;

import com.alibaba.fastjson.JSONObject;
import com.chenfan.process.dto.UserDTO;
import com.chenfan.process.entity.dto.InstanceSnapshotDTO;
import com.chenfan.process.entity.po.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author liuming
 * @date 2021/10/22
 */
public class ConvertUtils {

    public static boolean contains(String source, Long target) {
        String[] split = source.split(",");
        for (int i = 0; i < split.length; i++) {
            if (Objects.equals(split[i], target.toString())) {
                return true;
            }
        }
        return false;
    }

    public static String convert(String[] userId, String[] userName) {
        List<UserDTO> userList = new ArrayList<>();
        for (int i = 0; i < userId.length; i++) {
            userList.add(UserDTO.builder().userId(Long.valueOf(userId[i])).userName(userName[i]).build());
        }
        return JSONObject.toJSONString(userList);
    }

    public static String convert(String[] source) {
        return String.join(",", Arrays.asList(source));
    }

    // 并且当前的状态为审批中
    public static boolean replace(ProcessInstanceApprovePo processInstanceApprovePo, Long sourceId, Long targetId,
        String targetName) {
        boolean replace = false;
        if (Objects.equals(processInstanceApprovePo.getUserId(), sourceId)) {
            processInstanceApprovePo.setUserId(targetId);
            processInstanceApprovePo.setUserName(targetName);
            replace = true;
        }
        processInstanceApprovePo.setCreateTime(new Date());
        return replace;
    }

    // 并且当前的状态为审批中
    public static boolean replace(ApprovalPo approvalPo, List<ProcessInstanceApprovePo> processInstanceApproves, Long sourceId, Long targetId,
        String targetName) {
        boolean count = false;
        for (int j = 0; j < processInstanceApproves.size(); j++) {
            ProcessInstanceApprovePo processInstanceApprovePo = processInstanceApproves.get(j);
            if (processInstanceApprovePo.normal()) {
                boolean replaceResult = replace(processInstanceApprovePo, sourceId, targetId, targetName);
                if (replaceResult) {
                    count = true;
                }
            }
        }

        String userId = processInstanceApproves.stream().map(e -> e.getUserId().toString()).collect(Collectors.joining(","));
        String userName = processInstanceApproves.stream().map(e -> e.getUserId().toString()).collect(Collectors.joining(","));
        approvalPo.setUserId(userId);
        approvalPo.setUserName(userName);
        approvalPo.setUpdateTime(new Date());
        return count;

    }

    public static void replace(ApprovalPo approvalPo, Long sourceId, Long targetId, String targetName) {

        String[] userIdSplit = approvalPo.getUserId().split(",");
        String[] userNameSplit = approvalPo.getUserName().split(",");
        for (int i = 0; i < userIdSplit.length; i++) {
            if (Objects.equals(userIdSplit[i], sourceId.toString())) {
                userIdSplit[i] = targetId.toString();
                userNameSplit[i] = targetName;
            }
        }
        approvalPo.setUserId(ConvertUtils.convert(userIdSplit));
        approvalPo.setUserName(ConvertUtils.convert(userNameSplit));
        approvalPo.setUpdateTime(new Date());
    }

    public static void replace(ApprovalSnapshotPo approvalSnapshotPo, Map<Long, Long> nodeMap, Long userId, String userName) {
        String relation = approvalSnapshotPo.getRelation();

        InstanceSnapshotDTO instanceSnapshot = JSONObject.parseObject(relation, InstanceSnapshotDTO.class);
        // 找到对应的节点, 使用最新的人员进行替换
        List<ProcessPo> processList = instanceSnapshot.getProcessList();
        for(ProcessPo p: processList) {
            if (Objects.nonNull(nodeMap.get(p.getId()))) {
                p.setAssociateGroupId(userId.toString());
                p.setAssociateGroupName(userName);
            }
        }

        approvalSnapshotPo.setRelation(JSONObject.toJSONString(instanceSnapshot));
        approvalSnapshotPo.setCreateDate(new Date());
        approvalSnapshotPo.setUpdateDate(new Date());
    }

    public static void replace(ProcessDetailPo processDetailPo, Map<Long, ProcessPo> replaceMap) {
        ProcessPo processPo = replaceMap.get(processDetailPo.getNextProcessId());
        if (Objects.nonNull(processPo)) {
            processDetailPo.setNextProcessId(processPo.getId());
        }
        processDetailPo.setCreateTime(new Date());
    }
}
